#!/bin/bash

#check for required programs
type -P dmsetup &>/dev/null || { echo "dmestup rquired but not installed. Aborting." >&2; exit 1; }
type -P cryptsetup &>/dev/null || { echo "cryptsetup rquired but not installed. Aborting." >&2; exit 1; }

CRYPTDEV=mycrypt
REGUSER=chanux		#Change this to your login-name.
TEMPFILE=/tmp/crysp-$REGUSER

case "$1" in
mount)
	LOOPDEV=`losetup -f`
	echo $LOOPDEV > $TEMPFILE
	losetup $LOOPDEV $2
	cryptsetup luksOpen $LOOPDEV $CRYPTDEV
	mount /dev/mapper/$CRYPTDEV -rw $3
	chown -R $REGUSER $3			#workaround to allow nautilus r/w access to the mount
	;;
umount)
	LOOPDEV=`cat /tmp/crysp-chanux`
	sync
	umount $2
	cryptsetup luksClose $CRYPTDEV
	losetup -d $LOOPDEV
	;;
*)
	echo "Usage:"
	echo -e "\t $0 mount /loop/device /mount/point"
	echo -e "\t $0 umount /mount/point"
	
	;;
esac

exit 0
